package com.hushui.mall.coupon.controller;

import com.hushui.common.utils.PageUtils;
import com.hushui.common.utils.R;
import com.hushui.mall.coupon.entity.SeckillSessionEntity;
import com.hushui.mall.coupon.service.SeckillSessionService;
import com.hushui.mall.coupon.to.SecKillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 每日秒杀活动场次
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2023-03-15 12:12:22
 */
@CrossOrigin
@Controller
@RequestMapping("coupon/seckillsession")
public class SeckillSessionController {
    @Autowired
    private SeckillSessionService seckillSessionService;

    /**
     * 查询秒杀商品
     */
    @ResponseBody
    @GetMapping("getCurrentSeckillSkus")
    public R getCurrentSeckillSkus(){
        List<SecKillSkuRedisTo> vos = seckillSessionService.getCurrentSeckillSkus();
        return R.ok()
                .put("data", vos);
    }

    /**
     * 获取秒杀商品的详情
     */
    @ResponseBody
    @GetMapping("seckill/{skuId}")
    public R getSeckillSkuInfo(@PathVariable("skuId") Long skuId){
        SecKillSkuRedisTo secKillSkuRedisTo =  seckillSessionService.getSeckillSkuInfo(skuId);
        return R.ok()
                .put("data", secKillSkuRedisTo);
    }

    /**
     * 秒杀
     */
    @GetMapping("kill")
    public String kill(@RequestParam("killId") String killId,
                       @RequestParam("key") String key,
                       @RequestParam("num") Integer num,
                       Model model){
        // 检查是否登录


        //TODO 开始秒杀
        String orderSn = seckillSessionService.kill(killId, key, num);
        model.addAttribute("orderSn", orderSn);

        return "success";
    }

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = seckillSessionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		SeckillSessionEntity seckillSession = seckillSessionService.getById(id);

        return R.ok().put("seckillSession", seckillSession);
    }

    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    public R save(@RequestBody SeckillSessionEntity seckillSession){
		seckillSessionService.save(seckillSession);

        return R.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public R update(@RequestBody SeckillSessionEntity seckillSession){
		seckillSessionService.updateById(seckillSession);

        return R.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		seckillSessionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
